package cn.community.care.module.system.controller.admin.purchaseinfo;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.community.care.framework.common.pojo.PageParam;
import cn.community.care.framework.common.pojo.PageResult;
import cn.community.care.framework.common.pojo.CommonResult;
import cn.community.care.framework.common.util.object.BeanUtils;
import static cn.community.care.framework.common.pojo.CommonResult.success;

import cn.community.care.framework.excel.core.util.ExcelUtils;

import cn.community.care.framework.operatelog.core.annotations.OperateLog;
import static cn.community.care.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.community.care.module.system.controller.admin.purchaseinfo.vo.*;
import cn.community.care.module.system.dal.dataobject.purchaseinfo.PurchaseInfoDO;
import cn.community.care.module.system.service.purchaseinfo.PurchaseInfoService;

@Tag(name = "管理后台 - 采购信息")
@RestController
@RequestMapping("/system/purchase-info")
@Validated
public class PurchaseInfoController {

    @Resource
    private PurchaseInfoService purchaseInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建采购信息")
    public CommonResult<Long> createPurchaseInfo(@Valid @RequestBody PurchaseInfoSaveReqVO createReqVO) {
        return success(purchaseInfoService.createPurchaseInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新采购信息")
    public CommonResult<Boolean> updatePurchaseInfo(@Valid @RequestBody PurchaseInfoSaveReqVO updateReqVO) {
        purchaseInfoService.updatePurchaseInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除采购信息")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deletePurchaseInfo(@RequestParam("id") Long id) {
        purchaseInfoService.deletePurchaseInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得采购信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<PurchaseInfoRespVO> getPurchaseInfo(@RequestParam("id") Long id) {
        PurchaseInfoDO purchaseInfo = purchaseInfoService.getPurchaseInfo(id);
        return success(BeanUtils.toBean(purchaseInfo, PurchaseInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得采购信息分页")
    public CommonResult<PageResult<PurchaseInfoRespVO>> getPurchaseInfoPage(@Valid PurchaseInfoPageReqVO pageReqVO) {
        PageResult<PurchaseInfoDO> pageResult = purchaseInfoService.getPurchaseInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PurchaseInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出采购信息 Excel")
    @OperateLog(type = EXPORT)
    public void exportPurchaseInfoExcel(@Valid PurchaseInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PurchaseInfoDO> list = purchaseInfoService.getPurchaseInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "采购信息.xls", "数据", PurchaseInfoRespVO.class,
                        BeanUtils.toBean(list, PurchaseInfoRespVO.class));
    }

}